﻿using StrokeCenterSystem.Areas.StrokeCenter.Models;
using StrokeCenterSystem.Bll;
using StrokeCenterSystem.Code.Bll;
using StrokeCenterSystem.Core;
using StrokeCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace StrokeCenterSystem.Areas.StrokeCenter.Controllers
{
    public class PatientListController : BaseController
    {
        //
        // GET: /StrokeCenter/PatientList/

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 患者总览加载
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            //name=&status=0&pathway=0&cometype=0&page=1&pagesize=30
            int arrivedType = WebUtils.GetInt("cometype",-1);
            string Id = WebUtils.GetString("Id");
            string name = WebUtils.GetString("name");
            int status = WebUtils.GetInt("status",-1);
            int pathwayId = WebUtils.GetInt("pathway",-1);
            string startDate = WebUtils.GetString("startDate");
            string endDate = WebUtils.GetString("endDate");
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pageSize", 30);
            OperateResult resultInfo = PatientManager.GetPatientOverview(pageIndex, pageSize, name, status, pathwayId, arrivedType, Id, -1, startDate, endDate);
            string NoData = "{\"Total\": 0,\"Rows\": [], \"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            if (resultInfo.Result == 1)
            {
                try
                {
                    var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                    var result = new { Total = listData.PatientNum.Value, Rows = listData.PatientList, InHospitalNum = listData.InHospitalNum.Value, PreHospitalNum = listData.PreHospitalNum.Value, PathWayNum = listData.PathWayNum.Value };
                    return JsonDate(result);
                }
                catch
                { }
            }
            else
            {
                NoData = "{\"Total\": 0,\"Rows\":[],\"Result\":\"" + resultInfo.Result + "\",\"Body\":\"" + resultInfo.Body + "\",\"InHospitalNum\": 0, \"PreHospitalNum\": 0, \"PathWayNum\": 0}";
            }
            return Content(NoData);
        }

        /// <summary>
        /// 获取患者总览下拉列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPatientList()
        {
            OperateResult resultInfo = PatientManager.GetPatientOverview(1, 10000, "", -1, -1, -1, "");
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            string id = WebUtils.GetString("Id");
            ViewBag.Id = id;
            return View();
        }
        /// <summary>
        /// 根据ID保存一个患者 新增/编辑
        /// </summary>
        /// <returns></returns>
        public ContentResult Save()
        {
            PatientOverview overview = new PatientOverview();
            overview.PATIENT_SEX = WebUtils.GetString("eventGrade");//性别
            overview.Stroke_Diagnosis = WebUtils.GetInt("txtdiagnosisofchestpain");//卒中诊断
            overview.EMS_DISEASE_ADDR = WebUtils.GetString("txtillAddress").Trim();//发病地址
            string ems_disease_time = WebUtils.GetString("txtillTime").Trim();//发病时间
            if (!string.IsNullOrEmpty(ems_disease_time))
            {
                ems_disease_time += ":00";
                overview.EMS_DISEASE_TIME = Convert.ToDateTime(ems_disease_time);
            }
            overview.ID = WebUtils.GetString("txtId").Trim();//ID
            overview.PATIENT_AGE = WebUtils.GetString("txtAge").Trim();//年龄
            overview.PATIENT_ARRIVE_CLASS = WebUtils.GetString("txtfromhospitalType").Trim();//来院方式
            overview.AdmissionPath = WebUtils.GetString("txtcomeinType").Trim();//入院途径
            overview.Hospital_ID = WebUtils.GetString("txtLivePicId").Trim();//门诊ID
            overview.PATIENT_ID = WebUtils.GetString("txtPicaId").Trim();//住院ID
            overview.PATIENT_NAME = WebUtils.GetString("txtRealName").Trim();//患者姓名
            overview.PATIENT_PHONE = WebUtils.GetString("txtPhoneNum").Trim();//联系电话
            overview.PATIENT_ADDR = WebUtils.GetString("txtAddress").Trim();//住址
            overview.PATIENT_ID_NUM = WebUtils.GetString("txtIDCard").Trim();//身份证
            
            overview.DiagnosticPathId = 0;//诊疗路径ID
            overview.IsHelp = 0;//是否呼救
            string registerTime = WebUtils.GetString("txtCreateTime").Trim();//登记时间
            if (!string.IsNullOrEmpty(registerTime))
            {
                registerTime += ":00";
                overview.RegisterTime = Convert.ToDateTime(registerTime);
            }
            string oiverviewJson = JsonConvert.SerializeObject(overview).Trim();
            OperateResult resultInfo = PatientManager.SetPatientOverview(oiverviewJson);
            return JsonDate(resultInfo);
        }


        /// <summary>
        /// 获取诊疗路径名称
        /// ID Name
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPathNameList()
        {
            OperateResult resultInfo = PathwayManager.GetPathNameList();
            return Content(resultInfo.Body.ToString());
        }

        /// <summary>
        /// 作废患者
        /// </summary>
        /// <returns></returns>
        public ActionResult GiveUp()
        {
            string patientId = WebUtils.GetString("patientId");
            OperateResult resultInfo = new OperateResult();
            resultInfo = PatientManager.DeletePatientRecord(patientId);
            return JsonDate(resultInfo);
        }

        public ActionResult WristPrint()
        {
            string pid = WebUtils.GetString("pid");
            if (string.IsNullOrWhiteSpace(pid))
                return Content("缺少ID参数");
            ViewBag.pid = pid;
            return View();
        }
    }
}
